#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    int ans = 1, b = 1;
    sort(a.begin(), a.end());
    for(int i = 1; i < n; i++) {
        if(a[i] - a[i - 1] <= k) {
            b++;
            ans = max(ans, b);
        }else{
            b = 1;
        }
    }
    cout << n - ans << endl;
}

int main() {
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}